<template>
  <div class="page">
    <div class="head">
      <div class="header">
        <h1>{{ user.nickName }}</h1>
        <router-link tag="div" class="backhome" to="/home" replace></router-link>
      </div>
      <div class="address" v-clipboard="user.address" @success="isCopy(1)" @error="isCopy(0)" :key="1">{{ user.address }}</div>
    </div>
    <div class="filter">
      <ul class="status">
        <li v-for="(item, index) in category" :class="{'active': index == active}" @click="changeCategory(index)" :key="index">{{item}}</li>
      </ul>
    </div>
    <div class="panel" v-if="active == 0">
      <router-link tag="div" class="item" :to="`/monkey/${item.id}`" v-for="(item,index) in homeList" :key="index">
        <div class="img">
          <img :src="item.facade" :style="{background: item.background}">
          <div class="id"># {{ item.id }}</div>
          <div class="info">
            <p v-if="item.status">
              <i class="ico" :class="{'sell':item.type == 1,'love':item.type == 2}"></i>
              <span>{{ item.price }} 猴毛</span>
            </p>
            <p>
              <span>{{ item.grow | toFix(2) }}/</span><span>{{ item.bear | toFix(2) }}/</span><span>{{ item.makeMoney | toFix(2) }} · {{ item.weight | toFix(2) }}kg</span>
            </p>
          </div>
        </div>
        <div class="price">{{ item.generation }} {{ $t('代') }}</div>
      </router-link>
      <infinite-loading @infinite="initData" ref="infiniteLoading1">
        <span slot="no-more"></span>
        <div slot="no-results">
          <br>
          <empty></empty>
        </div>
        <spinner slot="spinner" type="lines"></spinner>
      </infinite-loading>
    </div>

    <div class="monkeys" v-else>
      <div class="panel" v-for="(item,index) in towerList" :key="index">
        <router-link :to="`/tower/${item.id}`" tag="div" class="list-item">
          <div class="img">
            <!-- castleImgUrl -->
            <img :src="item.castleImgUrl" :style="{background: '#eafee1'}">
          </div>
          <ul class="minfo">
            <li>{{ item.castleName }}（{{item.castleNickname}}）</li>
            <li>城主：{{item.castleOwnerName}}</li>
            <li>状态：
              <count-down :startTime="item.systemTime" :endTime="item.canAttackTime" endText="可攻击" surfixText="后可攻击"></count-down>
            </li>
            <li>上一小时收益：{{ item.profit || 0 }} 猴毛</li>
          </ul>
        </router-link>
      </div>
      <empty v-if="homeList.length == 0"></empty>
    </div>
  </div>
</template>

<script>
  import empty from './common/empty.vue'
  import InfiniteLoading from 'vue-infinite-loading';
  import CountDown from "./common/vue2-countdown.vue";
  import {
    Spinner
  } from 'vux'
  export default {
    components: {
      empty,
      InfiniteLoading,
      Spinner,
      CountDown
    },
    data() {
      return {
        current: 1,
        homeList: [],
        towerList: [],
        user: {},
        category: ['小猴', '城堡'],
        active: 0
      }
    },
    mounted() {
      this.$nextTick(function() {
        this.address = this.$route.params.address
      });
    },
    methods: {
      initData($state) {
        this.$http
          .post(`/game/personalCenterPage`, {
            current: this.current,
            address: this.address
          })
          .then(res => {
            if (res.code == 200) {
              this.user = res.result.condition
              if (res.result.total > 0) {
                this.homeList = this.homeList.concat(res.result.records);
                $state.loaded();
                if (res.result.current < res.result.pages) {
                  this.current += 1;
                } else {
                  $state.complete();
                }
              } else {
                $state.complete();
              }
            } else {
              this.$vux.toast.text(res.msg)
            }
          })
          .catch(function(error) {});
      },
      changeCategory(index) {
        this.active = index;
        if (index) {
          this.$http
            .get(`/gameCastle/query/${this.address}`)
            .then(res => {
              if (res.code == 200) {
                this.towerList = res.result;
              } else {}
            })
            .catch(function(error) {});
        } else {
          this.initData()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .panel {
    padding: 12px 8px;
  }
  .filter {
    display: flex;
    justify-content: space-between;
    white-space: nowrap; // flex-wrap: wrap;
    font-size: 12px;
    background: #fff;
    height: 40px;
    line-height: 40px;
    padding: 0 12px;
    margin: 0 8px 12px;
    border-radius: 4px;
    box-shadow: 3px 3px 10px 0px rgba(158, 158, 158, 0.4);
    overflow-x: scroll;
    overflow-y: hidden;
    &::-webkit-scrollbar {
      display: none;
    }
    ul {
      display: flex;
      align-items: center;
      li {
        cursor: pointer;
      }
    }
    .status {
      color: $grey;
      li {
        transition: all .3s;
      }
      li:not(:last-child) {
        margin-right: 12px;
      }
      .active {
        color: $green;
        font-size: 14px;
        border-bottom: 2px solid $green;
      }
    }
  }
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .address {
    color: $grey;
    font-size: 12px;
    word-break: break-all;
    position: relative;
    top: -10px;
  }
  .item {
    display: inline-block;
    vertical-align: middle;
    width: 33.33%; // min-height: 150px;
    border: 4px solid transparent;
    line-height: 1;
    overflow: hidden;
    min-height: 90px;
    .img {
      position: relative;
      width: 100%;
      font-weight: 300;
      min-height: 90px;
      background-color: #f9f7fc;
      font-size: 10px;
      border-radius: 4px;
      overflow: hidden;
      @media only screen and (min-width: 320px) {
        font-size: 8px;
      }
      @media only screen and (min-width: 375px) {
        font-size: 10px;
      }
      @media only screen and (min-width: 414px) {
        font-size: 12px;
      }
      @media only screen and (min-width: 768px) {
        font-size: 16px;
      }
      img {
        width: 100%;
      }
      .id {
        position: absolute;
        color: $grey;
        background-color: #fff;
        left: 0;
        top: 6px;
        padding: 3px 5px 3px 0;
        border-radius: 0 10px 10px 0;
      }
      .info {
        position: absolute;
        left: 0;
        bottom: 0;
        line-height: 14px;
        color: #000;
        white-space: nowrap;
        padding: 2px;
      }
    }
    .price {
      text-align: center;
      font-size: 12px;
      padding: 5px 10px;
      color: #333;
      @media only screen and (min-width: 320px) {
        font-size: 10px;
      }
      @media only screen and (min-width: 375px) {
        font-size: 12px;
      }
      @media only screen and (min-width: 414px) {
        font-size: 14px;
      }
      @media only screen and (min-width: 768px) {
        font-size: 16px;
      }
    }
  }
  .list-item {
    position: relative;
    display: flex;
    height: 100px;
    overflow: hidden;
    flex-shrink: 1;
    .img {
      width: 100px;
      height: 100px;
      font-weight: 300;
      margin-right: 10px;
      position: relative;
      flex-shrink: 0;
      font-size: 10px;
      border-radius: 4px;
      overflow: hidden;
      @media only screen and (min-width: 375px) {
        font-size: 10px;
      }
      @media only screen and (min-width: 414px) {
        font-size: 12px;
      }
      @media only screen and (min-width: 768px) {
        font-size: 14px;
      }
      .id {
        position: absolute;
        color: $grey;
        background-color: rgba(255, 255, 255, 0.8);
        left: 0;
        top: 6px;
        padding: 0 5px 0 0;
        border-radius: 0 100px 100px 0;
      }
      img {
        width: 100%;
      }
      .info {
        position: absolute;
        left: 2px;
        bottom: 2px;
        line-height: 14px;
        color: #000;
        width: 100%;
        white-space: nowrap;
        padding: 2px;
        p {
          width: 100%;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
    }
    .minfo {
      display: flex;
      flex: 1;
      overflow: hidden;
      justify-content: space-between;
      flex-direction: column;
      font-size: 14px;
      color: $grey;
      white-space: nowrap;
      li:first-child {
        color: #000;
      }
      li {
        overflow: hidden;
        text-overflow: ellipsis;
      }
      span {
        vertical-align: middle;
      }
    }
    .tag {
      position: absolute;
      top: -3px;
      right: 0;
      color: $green;
    }
  }
</style>
